﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace wojilu
{

    /// <summary>
    /// 显示消息提示对话框。
    /// </summary>
    public class MessageBox : System.Web.UI.UserControl
    {
        #region 字段
        /// <summary>
        /// 系统版本号
        /// </summary>
        public const string ASSEMBLY_VERSION = "0.58";

        /// <summary>
        /// 系统日期
        /// </summary>
        public const string ASSEMBLY_YEAR = "2010";
        /// <summary>
        /// 系统名称
        /// </summary>
        public const string SystemName = "一步半CMS系统";

        /// <summary>
        /// 官方网址
        /// </summary>
        public static string SystemUrl = "http://www.0760w.cn/";

        /// <summary>
        /// 系统版本号
        /// </summary>
        public static string SystemVersion = "YBBCMS.SQL v" + ASSEMBLY_VERSION;

        /// <summary>
        /// 编码
        /// </summary>
        public static string encoding = "utf-8";

        public static Encoding encode = Encoding.GetEncoding(encoding);

        #endregion

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="Control">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowConfirm(WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "');window.location=\"" + url + "\"</script>");
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirects(Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());
        }

        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");

        }

        /// <summary>
        /// 操作提示
        /// </summary>
        /// <param name="Str"></param>
        public static void ErrView(object Str)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<title>提示信息 - " + SystemName + "</title>");
            builder.AppendLine("<script language=\"javascript\" type=\"text/javascript\">");
            builder.AppendLine("        var P = window.parent, D = P.loadinndlg();");
            builder.AppendLine("</script>");
            builder.AppendLine("<table width=\"100%\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>");
            builder.AppendLine("<table align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"400\" style=\"border:1px #CCCCCC solid;\">");
            builder.AppendLine("<tr><td height=\"22\" style=\"border-bottom:1px #CCCCCC solid;padding:2px;background-color:#EFEFEF\">&nbsp;<b>友情提示：</b></td></tr>");
            builder.AppendLine("<tr><td height=\"180\">");
            builder.AppendLine("<table width='80%' height='100%' border=0 cellspacing=0 cellpadding=0 align=center><tr><td>");
            builder.AppendLine(Str.ToString());
            builder.AppendLine("<br><br>·<a href=# Onclick=\"javascript:history.back()\" style='cursor:pointer'>返回上一页</a>");
            builder.AppendLine("<br>·<a href='/' style='cursor:pointer'>返回首页</a>");
            builder.AppendLine("<br>·<a href=# Onclick=\"javascript:window.parent.close()\" style='cursor:pointer'>关闭当前页</a>");
            builder.AppendLine("</td></tr></table>");
            builder.AppendLine("</td></tr>");
            builder.AppendLine("<tr><td style='padding:10px 0 0 0;text-align:center;'><a href='" + SystemUrl + "' target=_blank style='color:#CCCCCC'>Powered By " + SystemVersion + "</a></td></tr>");
            builder.AppendLine("</table>");
            builder.AppendLine("</td></tr></table>");
            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 在客户端显示弹出对话框。
        /// </summary>
        /// <param name="msg">要显示的信息。</param>
        /// <param name="i">设置弹出对话框格式。0为只弹出对话框，1为弹出后并后退！2为弹出并关闭网页</param>
        public static void Alert(string msg, int i)
        {
            if (i == 0)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg + "');</script>");
                HttpContext.Current.Response.End();
            }
            if (i == 1)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg + "');history.back();</script>");
                HttpContext.Current.Response.End();
            }
            if (i == 2)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>alert('" + msg + "');window.close();</script>");
                HttpContext.Current.Response.End();
            }
        }

        /// <summary>
        /// 在客户端显示弹出对话框。
        /// </summary>
        /// <param name="name">脚本块标识。当同一页面要调用两个弹出框时需不同的标识，否则后者会覆盖前者。</param>
        /// <param name="msg">要显示的信息。</param>
        public static void Alert(string name, string msg)
        {
            Page page = new Page();
            page.ClientScript.RegisterStartupScript(page.GetType(), name, ("<script language='javascript'>alert('" + msg + "');</script>"));
        }

        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(System.Web.UI.Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="page">当前页面环境,一般为this</param>
        /// <param name="msg">要显示的信息</param>
        /// <param name="trueDoSomething">如果点确定后要做的事</param>
        /// <param name="falseDoSomething">如果点取消后要做的事</param>
        public static void ShowConfirm(System.Web.UI.Page page, string msg, string trueDoSomething, string falseDoSomething)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<SCRIPT language='JavaScript' defer>");
            Builder.Append("if(confirm ('" + msg + "')){");
            Builder.Append("" + trueDoSomething + "}");
            Builder.Append("else{");
            Builder.Append("" + falseDoSomething + "}");
            Builder.Append("</SCRIPT>");

            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        /// <param name="openPage">是否跳出框架</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url, bool openPage)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            if (openPage)
                Builder.AppendFormat("top.location.href='{0}'", url);
            else
                Builder.AppendFormat("location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());
        }

        /// <summary>
        /// 显示消息确认提示框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="hiddenfield_name">隐藏表单控件名</param>
        public static void confirm(System.Web.UI.Page page, string msg, string hiddenfield_name)
        {
            string sMsg = msg.Replace("\n", "\\n");
            sMsg = msg.Replace("\"", "'");

            StringBuilder sb = new StringBuilder();

            sb.Append(@"<INPUT type=hidden value='0' name='" +
              hiddenfield_name + "'>");

            sb.Append(@"<script language='javascript' defer>");

            sb.Append(@" if(confirm( """ + sMsg + @""" ))");
            sb.Append(@" { ");
            sb.Append("document.forms[0]." + hiddenfield_name + ".value='1';"
              + "document.forms[0].submit(); }");
            sb.Append(@" else { ");
            sb.Append("document.forms[0]." + hiddenfield_name + ".value='0'; }");

            sb.Append(@"</script>");

            page.ClientScript.RegisterStartupScript(page.GetType(), "message", sb.ToString());
        }

        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="key">标识关键字</param>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(string key, System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), key, "<script language='javascript' defer>" + script + "</script>");
        }

        /// <summary>
        /// 页面重载
        /// </summary>
        public static void Location()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\"> \n");
            sb.Append("window.location.href=window.location.href;");
            sb.Append("</script>");
            System.Web.HttpContext.Current.Response.Write(sb.ToString());

        }


        /// <summary>
        /// 显示一个弹出窗口，并刷新转向上一页
        /// </summary>
        /// <param name="str"></param>
        public static void ShowPre(string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\" defer> \n");
            sb.Append("alert(\"" + str.Trim() + "\"); \n");
            sb.Append("var p=document.referrer; \n");
            sb.Append("window.location.href=p;\n");
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
        }

        /// <summary>
        /// 显示一个弹出窗口，不刷新转向上一页
        /// </summary>
        /// <param name="str"></param>
        public static void ShowBack(string str)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\" defer> \n");
            sb.Append("alert(\"" + str.Trim() + "\"); \n");
            sb.Append("history.back();\n");
            sb.Append("</script>");

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
        }

        /// <summary>
        /// 在客户端执行一段脚本。
        /// </summary>
        /// <param name="name">脚本框提示或者弹出信息。</param>
        /// <param name="cmd">要执行的命令或者要跳转到页面。</param>
        /// <param name="i">0则cmd为正常要执行的命令，1则cmd为需要跳转到的页面,2为在当前框架内跳转,3为弹出对话框并执行指定的命令。</param>
        public static void JsExe(string name, string cmd, int i)
        {
            if (i == 0)
            {
                HttpContext.Current.Response.Write("<script language='javascript'>" + cmd + ";<script>");
                HttpContext.Current.Response.End();
            }
            if (i == 1)
            {
                HttpContext.Current.Response.Write("<script>alert('" + name + "');parent.document.location.href='" + cmd + "';</script>");
                HttpContext.Current.Response.End();
            }
            if (i == 2)
            {
                HttpContext.Current.Response.Write("<script>alert('" + name + "');this.document.location.href='" + cmd + "';</script>");
                HttpContext.Current.Response.End();
            }
            if (i == 3)
            {
                HttpContext.Current.Response.Write("<script>alert('" + name + "');" + cmd + ";</script>");
                HttpContext.Current.Response.End();
            }
        }

        /// <summary>
        /// 在客户端执行一段脚本。
        /// </summary>
        /// <param name="name">脚本框提示或者弹出信息。</param>
        /// <param name="cmd">要执行的命令或者要跳转到页面。</param>
        /// <param name="framename"></param>
        public static void JsExe(string name, string cmd, string framename)
        {
            HttpContext.Current.Response.Write("<script>alert('" + name + "');this.document.location.href='" + cmd + "';window.parent.frames[\"" + framename + "\"].location.reload();</script>");
            HttpContext.Current.Response.End();
        }

        #region 弹出JS窗口提示框
        /// <summary>
        /// 弹出JS窗口提示框
        /// </summary>
        /// <param name="message">提示消息</param>
        /// <param name="url">URL地址，可选参数，为空则只弹出对话框，而不刷新页面</param>
        public static void JsWindows(string message, string url)
        {
            if (url == "")
            {
                HttpContext.Current.Response.Write("<script>alert('" + message + "')</script>");
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('" + message + "');window.location='" + url + "'</script>");
            }
        }
        #endregion

        /// <summary>
        /// 提示错误信息并保存
        /// </summary>
        /// <param name="mes"></param>
        public static void SaveLog(object mes)
        {
            string path = "/_data/log/log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            if (!FileSystemObject.IsExist(PathHelper.Map(path), FsoMethod.File))
            {
                FileSystemObject.Create(PathHelper.Map(path), FsoMethod.File);
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendLine(System.DateTime.Now.ToString());
            builder.AppendLine("\tIP 地 址：" + IPHelp.GetIPAddress());
            builder.AppendLine("\t浏 览 器：" + HttpContext.Current.Request.Browser.Browser + HttpContext.Current.Request.Browser.Version);
            builder.AppendLine("\t提示信息：" + mes);
            builder.AppendLine("\t提示时间：" + DateTime.Now);
            FileSystemObject.WriteAppend(PathHelper.Map(path), builder.ToString());
        }

        #region lhgcore
        /// <summary>
        /// 弹出窗口
        /// </summary>
        /// <param name=" _PageUrl"></param>
        /// <param name="_StrPage"></param>
        public static void _Alert(string _PageUrl, Page _StrPage)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("J.dialog.get({ id: 'But_Sumbit',title:'一步半管理系统提醒您：', width: 400, height: 150, page: '" + _PageUrl + "', cover: true, nodrag: true, noresize: true });");
            StrBuild.Append("</script>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }

        /// <summary>
        /// 弹出窗口
        /// </summary>
        /// <param name="_StrStyle"></param>
        /// <param name="_StrContent"></param>
        /// <param name="_StrPage"></param>
        public static void _Alert(string _StrStyle, string _StrContent, Page _StrPage)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("J.dialog.get({ id: 'But_Sumbit',title:'一步半管理系统提醒您：', width: 400, height: 150, html: '<div id=\"Div_Dialog\"><span class=\"" + _StrStyle + "\">" + _StrContent + "</span></div>', cover: true, nodrag: true, noresize: true });");
            StrBuild.Append("</script>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }

        /// <summary>
        /// 弹出窗口,跳转到指定页面,取消页尾
        /// </summary>
        /// <param name="_StrStyle"></param>
        /// <param name="_StrContent"></param>
        /// <param name="_StrPage"></param>
        /// <param name="gotoUrl"></param>
        public static void _Alert(string _StrStyle, string _StrContent, Page _StrPage, string gotoUrl)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("J.dialog.get({ id: 'But_Sumbit',title:'一步半管理系统提醒您：', width: 400, height: 150, html: '<div id=\"Div_Dialog\"><span class=\"" + _StrStyle + "\">" + _StrContent + "</span></div>', cover: true, nodrag: true, noresize: true, nofoot: true });");
            StrBuild.Append("</script>");
            StrBuild.Append("<meta http-equiv=refresh content='3;URL=" + gotoUrl + "'>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }

        /// <summary>
        /// 弹出窗口,指定弹出窗口的ID值
        /// </summary>
        /// <param name="id"></param>
        /// <param name="_StrStyle"></param>
        /// <param name="_StrContent"></param>
        /// <param name="_StrPage"></param>
        public static void _Alert(string id, string _StrStyle, string _StrContent, Page _StrPage)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("J.dialog.get({ id: '" + id + "',title:'一步半管理系统提醒您：', width: 400, height: 150, html: '<div id=\"Div_Dialog\"><span class=\"" + _StrStyle + "\">" + _StrContent + "</span></div>', cover: true, nodrag: true, noresize: true });");
            StrBuild.Append("</script>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }

        /// <summary>
        /// 弹出窗口
        /// </summary>
        /// <param name="_StrID">弹出窗口ID</param>
        /// <param name="_StrTitle">弹出窗口标题</param>
        /// <param name="_StrType">弹出窗口类型 html, Page, Link</param>
        /// <param name="_StrStyle">弹出窗口样式 Div_DialogOK, Div_DialogER</param>
        /// <param name="_StrContent">弹出窗口内容</param>
        /// <param name="_StrConver">弹出窗口是否使用遮罩层</param>
        /// <param name="_StrWidth">弹出窗口宽度</param>
        /// <param name="_StrHeight">弹出窗口高度</param>
        /// <param name="_StrPage">引用Page页</param>
        public static void _Alert(string _StrID, string _StrTitle, string _StrType, string _StrStyle, string _StrContent, string _StrConver, int _StrWidth, int _StrHeight, Page _StrPage)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("J.dialog.get({ id: '" + _StrID + "', title: '" + _StrTitle + "',  width: " + _StrWidth + ", height: " + _StrHeight + ", " + _StrType + ": '<div id=\"Div_Dialog\"><span class=\"" + _StrStyle + "\">" + _StrContent + "</span></div>', cover: " + _StrConver + "});");
            StrBuild.Append("</script>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }

        /// <summary>
        /// 弹出子窗口
        /// </summary>
        /// <param name="_StrStyle"></param>
        /// <param name="_StrContent"></param>
        /// <param name="_StrPage"></param>
        public static void _AlertDlg(string _StrStyle, string _StrContent, Page _StrPage)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("D.J.dialog.get({ id: 'But_AlertDlg', width: 400, height: 150, html: D.J.ie ? '<div id=\"Div_Dialog\"><span class=\"" + _StrStyle + "\">" + _StrContent + "</span></div>' : '11', cover: true, nodrag: true, noresize: true });");
            StrBuild.Append("</script>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }

        /// <summary>
        /// 上传后向父窗口传值并自动关闭
        /// D.J('父窗口路径ID').val( ','+D.J('#子窗口ID',document).value +D.J('子窗口ID的值').value );
        /// </summary>
        /// <param name="_StrPValue"></param>
        /// <param name="_StrDValue"></param>
        /// <param name="_StrPage"></param>
        public static void _GetPValue(string _StrPValue, string _StrDValue, Page _StrPage)
        {
            StringBuilder StrBuild = new StringBuilder();
            StrBuild.Append("<script type=\"text/javascript\" language=\"javascript\">");
            StrBuild.Append("D.J('" + _StrPValue + "').val( D.J('#" + _StrDValue + "',document).value );");
            StrBuild.Append("P.cancel();");  //这句就是上传后自动关闭的，去掉则不关闭
            StrBuild.Append("</script>");
            string StrJS = StrBuild.ToString();
            if (!_StrPage.ClientScript.IsStartupScriptRegistered(_StrPage.GetType(), "window.onload"))
            {
                _StrPage.ClientScript.RegisterStartupScript(_StrPage.GetType(), "window.onload", StrJS);
            }
        }


        #endregion
    }
}
